package com.wanmait.teatrain.controller;

import com.wanmait.teatrain.entity.TrainingResource;
import com.wanmait.teatrain.service.TrainingResourceService;
import com.wanmait.teatrain.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/trainingResource")
public class TrainingResourceController {

    @Resource
    private TrainingResourceService trainingResourceService;

    //获取前端请求的培训资源上传 --徐钰淞
    @RequestMapping("insert")
    public AjaxResult insert(@RequestParam("resourceName") String resourceName,
                             @RequestParam("isDocument") Boolean isDocument,
                             @RequestParam("isSpeed") Boolean isSpeed,
                             @RequestParam("resourceTotalDuration") Integer resourceTotalDuration,
                             @RequestParam("resourceDesc") String resourceDesc,
                             @RequestParam(value = "tagId") Integer tagId,
                             @RequestParam("adminId") Integer adminId,
                             @RequestPart("file") MultipartFile file) {
        TrainingResource trainingResource = new TrainingResource();
        trainingResource.setCreatAdminId(adminId);
        trainingResource.setResourceName(resourceName);
        trainingResource.setIsDocument(isDocument);
        trainingResource.setIsSpeed(isSpeed);
        trainingResource.setResourceTotalDuration(resourceTotalDuration);
        trainingResource.setResourceDesc(resourceDesc);
        trainingResource.setTagId(tagId);
        return trainingResourceService.insert(trainingResource,file);
    }
    @GetMapping("list")
    public AjaxResult getResourceList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String resourceName) {  // 新增搜索参数

        Map<String, Object> result = trainingResourceService.getResourceList(pageNum, pageSize, resourceName);
        return AjaxResult.success(result);
    }

    @GetMapping("allList")
    public AjaxResult getAllResourceList() {
        List<TrainingResource> resourceList = trainingResourceService.list();
        return AjaxResult.success().put("resourceList", resourceList);
    }

    // 添加更新资源接口
    @PostMapping("update")
    public AjaxResult updateResource(@RequestBody TrainingResource trainingResource) {
        try {
            // 设置更新时间
            trainingResource.setUpdateTime(new Date());
            // 从当前登录用户获取更新人ID（实际项目中需要根据你的认证机制获取）
            // 这里假设你有获取当前登录用户ID的方法
            // trainingResource.setUpdateAdminId(getCurrentAdminId());

            boolean success = trainingResourceService.updateById(trainingResource);
            if (success) {
                return AjaxResult.success("资源更新成功");
            } else {
                return AjaxResult.error("资源更新失败，资源不存在或未修改");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("资源更新失败：" + e.getMessage());
        }
    }
}